package com.itheima.reggie.handler;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


//全局异常处理器
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    //处理唯一值重复异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultInfo handlerDuplicateKeyException(Exception e){
        //打印日志
        e.printStackTrace();
        if(e.getMessage().contains("idx_category_name")){
            return ResultInfo.error("分类名称重复！");
        }
        //给前端返回提示
        return ResultInfo.error("填写值重复！");
    }


    //处理自定义业务异常
    @ExceptionHandler(CustomException.class)
    public ResultInfo handlerCustomException(Exception e){
        //打印日志
        e.printStackTrace();
        //给前端返回提示
        return ResultInfo.error(e.getMessage());
    }


    //非预期异常
    @ExceptionHandler(Exception.class)
    public ResultInfo handlerException(Exception e){
        e.printStackTrace();//打印日志
        return ResultInfo.error("服务器开小差了，请稍后再试！");//给前端返回提示
    }



}
